﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Windows.Markup;
using SilverTile.Xaml;
using SilverTile.ExtensionMethods;
using SilverTile;
using SilverTile.Sprites;

namespace SilverTileTests
{

    [TestClass]
    public class ResourceDictionaryExtensionTests
    {

        private string _xaml =
          @"
            <ResourceDictionary
                xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" 
                xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
                xmlns:st=""clr-namespace:SilverTile.Xaml;assembly=SilverTile"">

                <st:SpriteAnimation x:Key=""Player"" Fps=""20"">
                    <st:FrameAnimation Name=""Left"">
                            <st:SpriteFrame Source=""/SilverTileTests;component/Tiles/0002.png"" />
                    </st:FrameAnimation>
                    <st:FrameAnimation Name=""Right"">
                            <st:SpriteFrame Source=""/SilverTileTests;component/Tiles/0002.png"" />
                    </st:FrameAnimation>
                </st:SpriteAnimation>

                <st:SpriteFrame x:Key=""NotSprite"" Source=""/SilverTileTests;component/Tiles/0002.png"" />
        
            </ResourceDictionary>
            ";

        [TestMethod]
        public void GetSprite_Should_Return_A_Sprite_With_The_Correct_Properties_Set()
        {

            var resources = (ResourceDictionary)XamlReader.Load(_xaml);

            AnimatedSprite sprite = resources.GetSprite<AnimatedSprite>("Player");

            Assert.AreEqual(20, sprite.Fps, "Fps was not read correctly");
            Assert.AreEqual(2, sprite.Animations.Count, "Incorrect animations read");

        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void GetSprite_With_Invalid_Key_Should_Throw_An_Exception()
        {

            var resources = (ResourceDictionary)XamlReader.Load(_xaml);
            AnimatedSprite sprite = resources.GetSprite<AnimatedSprite>("InvalidKey");

        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void GetSprite_For_Resource_Of_Wrong_Type_Should_Throw_An_Exception()
        {

            var resources = (ResourceDictionary)XamlReader.Load(_xaml);
            AnimatedSprite sprite = resources.GetSprite<AnimatedSprite>("NotSprite");

        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void GetSprite_For_Invalid_Sprite_Type_Should_Throw_An_Exception()
        {

            var resources = (ResourceDictionary)XamlReader.Load(_xaml);
            AnimatedSprite sprite = resources.GetSprite<AnimatedSpriteWithInvalidConstructor>("Player");

        }

        public class AnimatedSpriteWithInvalidConstructor : AnimatedSprite
        {

            public AnimatedSpriteWithInvalidConstructor(string whatever)
                : base(32, 32)
            { }
        }

    }
}
